package cn.pku.sirx.util;

import org.springframework.util.Assert;

/**
 * @author WangXiao
 */
public class MathUtils {

	/**
	 * Get value of e^x/(e^x+1).
	 * 
	 * @param x
	 *            a double value, when x is too small or too large, the returned
	 *            value will not be sensitive
	 * @return value of e^x/(e^x+1)
	 */
	public static double binaryLogisticRegression(double x) {
		double b = Math.exp(x);
		return b / (1 + b);
	}

	/**
	 * Standardize a given vector and return the result, the given vector won't
	 * be modified.
	 * 
	 * @param vector
	 *            array of double elements
	 * @return standardized vector
	 */
	public static double[] standardizeVector(double[] vector) {
		Assert.notNull(vector);
		/*
		 * calculate vector length
		 */
		double vectorLength = .0;
		for (int i = 0; i < vector.length; i++) {
			vectorLength += vector[i] * vector[i];
		}
		vectorLength = Math.sqrt(vectorLength);
		/*
		 * standardization
		 */
		double[] result = new double[vector.length];
		for (int i = 0; i < vector.length; i++) {
			result[i] = vector[i] / vectorLength;
		}
		return result;
	}

}
